package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tmemberrates;
import com.xy.p2p.service.TmemberratesService;



@Controller
public class TmemberratesAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberratesAction.class);
	public TmemberratesAction() {
	}
 
 
 	/**
	 * 会员费率表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberratesAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmemberrates", new Tmemberrates());
        setDict(model);
		return "tmemberratesEdit";
	}

	/**
	 * 会员费率表新增保存
	 * 
	 * @param tmemberrates
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmemberratesSave.do")
	public String save(Tmemberrates tmemberrates, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmemberrates, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmemberrates = tmemberratesService.save(tmemberrates);
//		Tmemberrates tmemberrates2 = tmemberratesService.getFee(Tmemberrates.RECHARGE_FEE);
//		tmemberrates2.getRates();*asdf;asldkjf;laskdjf;laksdjf
		return "redirect:v_tmemberratesList.do";
	}

	/**
	 * 会员费率表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberratesDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberratesService.remove(id);
			}
		}
		return "redirect:v_tmemberratesList.do";
	}

	/**
	 * 会员费率表编辑页面
	 * 
	 * @param mrid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberratesEdit.do")
	public String edit(long mrid, HttpServletRequest request, ModelMap model) {
		Tmemberrates tmemberrates = tmemberratesService.get(mrid);
		model.addAttribute("tmemberrates", tmemberrates);
        setDict(model);
		return "tmemberratesEdit";
	}

	/**
	 * 会员费率表详情
	 * 
	 * @param mrid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberratesDetail.do")
	public String detail(long mrid, HttpServletRequest request, ModelMap model) {
		Tmemberrates tmemberrates = tmemberratesService.get(mrid);
		model.addAttribute("tmemberrates", tmemberrates);
        setDict(model);
		return "tmemberratesView";
	}

	/**
	 * 会员费率表查询（分页）
	 * 
	 * @param tmemberrates
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberratesList.do")
	public String listPage(Tmemberrates tmemberrates, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberratesService.getTmemberratesByPage(toMap(tmemberrates),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberratesList";
	}

	/**
	 * 会员费率表查询
	 * 
	 * @param tmemberrates
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberratesList1.do")
	public String list(Tmemberrates tmemberrates, HttpServletRequest request, ModelMap model) {
		List<Tmemberrates> list = tmemberratesService.getTmemberrates(toMap(tmemberrates));
		model.addAttribute("list", list);
        setDict(model);
		return "tmemberratesList";
	}
	
	/*
	 * 查询
	 * */
	@RequestMapping("v_tmemberratesList2.do")
	public String list1(HttpServletRequest request, ModelMap model) {
		Tmemberrates tmemberrates = new Tmemberrates();
		List<Tmemberrates> list= tmemberratesService.getTmemberrates(toMap(tmemberrates));
		if(list.size()>0)
			model.addAttribute("list", list);
        setDict(model);
		return "ratesInfo";
	}
	/*
	 * 新增
	 * */
	@RequestMapping("o_tmemberratesSave2.do")
	public String save1(Tmemberrates tmemberrates, HttpServletRequest request, ModelMap model) {
 		tmemberrates = tmemberratesService.save(tmemberrates);
		return "redirect:v_tmemberratesList.do";
	}
	/*
	 * 修改
	 * */
	@RequestMapping("o_tmemberratesEdit2.do")
	public String toAdd(HttpServletRequest request, ModelMap model) {
		String id = null;
		Tmemberrates tmemberrates = new Tmemberrates();
		id = request.getParameter("id");
		tmemberrates.setMrid(Long.parseLong(id));
		List<Tmemberrates> list= tmemberratesService.getTmemberrates(toMap(tmemberrates));
		model.addAllAttributes(toMap(list.get(0)));
		return "ratesInfoEdit";
	}
	


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberrates
	 * @return
	 */	private Map<String,Object> toMap(Tmemberrates tmemberrates){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberrates.getMrid()!=null&&!tmemberrates.getMrid().equals("")){
			map.put("mrid", tmemberrates.getMrid());
		}
		if(tmemberrates.getFeedcode()!=null&&!tmemberrates.getFeedcode().equals("")){
			map.put("feedcode", tmemberrates.getFeedcode());
		}
		if(tmemberrates.getFeedname()!=null&&!tmemberrates.getFeedname().equals("")){
			map.put("feedname", tmemberrates.getFeedname());
		}
		if(tmemberrates.getRates()!=null&&!tmemberrates.getRates().equals("")){
			map.put("rates", tmemberrates.getRates());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberrates tmemberrates, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberratesService tmemberratesService;   
}